function [ best_c,best_g ] = RBF_grid(train_matrix,label_matrix,c_begin,c_end,c_step,g_begin,g_end,g_step,K)
%RBF_GRID Summary of this function goes here
%train function 
%  Inputs:
%       model_name              the model that we use for training 
%       train_matrix(x,y,z)     contains z band image of size [x,y]
%       label_matrix(x,y)       is label matrix of train_matrix
%       param                   a parameter string used for svm training
%       g_begin,g_end           g's minimum value and max value
%       c_begin,c_end           C's minimum value and max value
%       c_step,g_step           step to search the grid
%       K                       K fold cross validation
%       
%  Outputs:
%
%    	model                   model generated by classifier
%
%   Note: this function needs libsvm installed

%reshape the data 
train_matrix=double(train_matrix);
label_matrix=double(label_matrix);
[x,y,band]=size(train_matrix);

train_feature=reshape(train_matrix,x*y,band);
train_label=reshape(label_matrix,x*y,1);
train_feature=sparse(train_feature);
disp('data preprocess finished')

%search the grid
disp('start searching ...')
max_accu=0;
for c=c_begin:c_step:c_end
    for g=g_begin:g_step:g_end
        param=['-t 2',' -c ',num2str(c),' -g ',num2str(g),' -v ',num2str(K)];
        accuracy=svmtrain(train_label,train_feature,param);
        if accuracy>max_accu
            best_c=c;
            best_g=g;
        end
    end
    
end
disp('searching finished')
end

